var util = require('../../utils/util.js');
var WXBizDataCrypt = require('../../crypto/WXBizDataCrypt.js');
let app = getApp();
let url = app.url;
let gobalData = app.gobalData;
// 存储用户信息
let userInfo = {};
userInfo.city = gobalData.city;
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		showModal: {
			type: Boolean,
			value: false,
		},
		leaderId: {
			type: null,
			value: null
		},
		userData: {
			type: Object,
			value: {}
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		empowerFlag: false
	},
	created() {
		
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		// 显示模态对话框
		showModal: function() {
			this.setData({
				showModal: true
			});
		},
		// 隐藏模态对话框
		hideModal: function() {
			this.setData({
				showModal: false
			});
		},
		toggleEmpowerTwo() {
			this.setData({
				empowerFlag: true
			})
		},
		goEmpower: function() {
			this.setData({
				showModal: true
			})
		},
		// 跳转到短信验证界面
		goshortMessage() {
			wx.navigateTo({
				url: "/pages/shortMessage/index"
			})
		},
		// 获取用户信息
		onGetUserInfo: function(e) {
			if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
				wx.showModal({
					title: '提示',
					showCancel: false,
					content: '您取消了授权',
				})
			} else {
				// 获取用户信息  存储后台需要的数据
				userInfo.name = gobalData.name = e.detail.userInfo.nickName;
				userInfo.gender = gobalData.gender = e.detail.userInfo.gender;
				userInfo.avatar = gobalData.avatar = e.detail.userInfo.avatarUrl;
				userInfo.openid = gobalData.openid;
				if (gobalData.name != "" && gobalData.gender != "" && gobalData.avatar != "") {
					this.setData({
						empowerFlag: true
					})
				}
			}

		},
		// 获取用户手机号
		onGetPhoneNumber: function(e) {
			let self = this;
			if (this.data.empowerFlag == false) {
				wx.showToast({
					title: '授权失败\n请先操作上一步',
					icon: 'none'
				})
			} else {
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					wx.showModal({
						title: '提示',
						showCancel: false,
						content: '您取消了授权',
						success: function(res) {}
					})
				} else {
					// 用户同意授权   ---解密手机号
					var pc = new WXBizDataCrypt(gobalData.appId, gobalData.session_key)
					var data = pc.decryptData(e.detail.encryptedData, e.detail.iv)
					// console.log('解密后 data: ', data); //解密手机号
					// 将获取到的手机号存储起来
					gobalData.mobile = data.phoneNumber;
					userInfo.mobile = gobalData.mobile;
					// 将数据发送给后台进行注册
					userInfo.leaderId = self.data.leaderId;
					userInfo.registerType = 1; // 微信一键授权登录
					util(`${url}user/register`, userInfo).then(res => {
						if (res.status == 1) {
							wx.showToast({
								title: '注册成功！'
							})
							// 关闭模态框
							this.hideModal();
							// 存储到当前页面
							this.setData({
								userInfo: userInfo,
								showModal: false
							})
							this.triggerEvent('userData', res);
							// 保存到缓存
							wx.setStorage({
								key: 'userInfo',
								data: res.content
							})
							gobalData.cid = res.content.userInfo.id;
							// 已经注册过。-------  可删除，99.99%不会出现这种情况
						} else if (res.msg == '你已经注册！') {
							util(`${url}user/login`, {
								openid: gobalData.openid
							}).then(res => {
								wx.setStorage({
									key: 'userInfo',
									data: res.content
								})
								this.setData({
									userInfo: userInfo,
									showModal: false
								})
								gobalData.cid = res.content.userInfo.id
							})
							// 其它未知错误---- 跳转都短信验证界面
						} else {
							wx.navigateTo({
								url: "/pages/shortMessage/index"
							})
						}
					})

				}
			}
		},


	}
})
